package com.czh.servlet.Provider;

import com.czh.pojo.Provider;
import com.czh.service.Provider.ProviderService;
import com.czh.service.Provider.ProviderServiceImpl;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ProviderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        String method = req.getParameter("method");
        if(method.equals("query")){
            providerList(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
    protected void providerList(HttpServletRequest req, HttpServletResponse resp){
        String queryProCode = req.getParameter("queryProCode");
        String queryProName = req.getParameter("queryProName");
        if (StringUtils.isNullOrEmpty(queryProName)) {
            queryProName = null;
        }
        if (StringUtils.isNullOrEmpty(queryProCode)) {
            queryProCode = null;
        }
        ProviderService providerService = new ProviderServiceImpl();
        List<Provider> providerList = providerService.getProviderList(queryProCode, queryProName);
        try {
            req.setAttribute("providerList", providerList);
            req.setAttribute("queryProName", queryProName);
            req.setAttribute("queryProCode", queryProCode);
            req.getRequestDispatcher("providerlist.jsp").forward(req,resp);
        } catch (ServletException | IOException e) {
            throw new RuntimeException(e);
        }
    }
}
